package com.gxuwz.medical.domain.apply;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.family.FamilyMember;
import com.gxuwz.medical.tools.DateUtil;

public class Apply {
	
	private String id;
	
	private FamilyMember membercode;
	
	private Chronicdis illcode;
	
	private String allcost;
	
	private String hosinvoice;
	
	private String seetime;
	
	private String applymoney;
	
	private String alreadgot;
	
	private String creator;
	
	private java.util.Date applytime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public FamilyMember getMembercode() {
		return membercode;
	}

	public void setMembercode(FamilyMember membercode) {
		this.membercode = membercode;
	}

	public Chronicdis getIllcode() {
		return illcode;
	}

	public void setIllcode(Chronicdis illcode) {
		this.illcode = illcode;
	}

	public String getAllcost() {
		return allcost;
	}

	public void setAllcost(String allcost) {
		this.allcost = allcost;
	}

	public String getHosinvoice() {
		return hosinvoice;
	}

	public void setHosinvoice(String hosinvoice) {
		this.hosinvoice = hosinvoice;
	}

	public String getSeetime() {
		return seetime;
	}

	public void setSeetime(String seetime) {
		this.seetime = seetime;
	}

	public String getApplymoney() {
		return applymoney;
	}

	public void setApplymoney(String applymoney) {
		this.applymoney = applymoney;
	}

	public String getAlreadgot() {
		return alreadgot;
	}

	public void setAlreadgot(String alreadgot) {
		this.alreadgot = alreadgot;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public java.util.Date getApplytime() {
		return applytime;
	}

	public void setApplytime(java.util.Date applytime) {
		this.applytime = applytime;
	}

	@Override
	public String toString() {
		return "Apply [id=" + id + ", cmscode=" + membercode + ", illcode="
				+ illcode + ", allcost=" + allcost + ", hosinvoice="
				+ hosinvoice + ", seetime=" + seetime + ", applymoney="
				+ applymoney + ", alreadgot=" + alreadgot + ", creator="
				+ creator + ", applytime=" + applytime + "]";
	}

	public Apply(String id, FamilyMember membercode, Chronicdis illcode,
			String allcost, String hosinvoice, String seetime,
			String applymoney, String alreadgot, String creator,
			java.util.Date applytime) {
		super();
		this.id = id;
		this.membercode = membercode;
		this.illcode = illcode;
		this.allcost = allcost;
		this.hosinvoice = hosinvoice;
		this.seetime = seetime;
		this.applymoney = applymoney;
		this.alreadgot = alreadgot;
		this.creator = creator;
		this.applytime = applytime;
	}
	
	public Apply(){
		
	}
	
	/**
	 * 添加慢病证信息
	 * @throws Exception
	 */
	public void addApply()throws Exception{
		Connection conn=null;
		try{
			
			Date creattime=new Date();
			String no=DateUtil.format(creattime, DateUtil.DFMT_YYYYMMDDHHMMSS);
			//慢病档案编号
			this.id=no;
			//1:获取连接实例
			conn=DbUtil.getConn();
		    //2:持久化数据
			saveToDB(conn);
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			//关闭连接
			DbUtil.close(conn);
		}
		
		
	}
	/**
	 * 持久化数据
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{

		  PreparedStatement pstmt=null;
		  try{
			  StringBuffer sqlBuff=new StringBuffer("insert into t_apply (id,membercode,illcode,allcost,hosinvoice,seetime,applymoney,alreadgot,creator,applytime)");
			  			   sqlBuff.append("values (?,?,?,?,?,?,?,?,?,?)");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			  int index=1;
			  pstmt.setString(index++, this.id);
			  pstmt.setString(index++, this.membercode.getMembercode());
			  pstmt.setString(index++, this.illcode.getIllcode());
			  pstmt.setString(index++, this.allcost);
			  pstmt.setString(index++, this.hosinvoice);
			  java.sql.Date applytime=new java.sql.Date(this.applytime.getTime());  //把java.util.Date转换成java.sql.Date
			  pstmt.setString(index++, this.seetime);
			  pstmt.setString(index++, this.applymoney);
			  pstmt.setString(index++, this.alreadgot);
			  pstmt.setString(index++, this.creator);
			  pstmt.setDate(index++, applytime);
			  int count =pstmt.executeUpdate(); 
		  }catch(SQLException e){
			  e.printStackTrace();
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
}
}
